#ifndef __EAGLWindow_H__
#define __EAGLWindow_H__

#include "U2RenderWindow.h"

#ifdef __OBJC__
//#import <UIKit/UIKit.h>
#import "U2EAGLView.h"
#import "U2EAGLViewController.h"

// Forward declarations
@class CAEAGLLayer;

// Define the native window type
typedef UIWindow *NativeWindowType;

#endif


U2EG_NAMESPACE_BEGIN


class EAGLSupport;
class EAGLESContext;

class _U2GLESPrivate EAGLWindow : public U2RenderWindow
{
protected:
	bool mClosed;
	bool mVisible;
	/// Is this using an external window handle?
	bool mIsExternal;
	/// Is this using an external view handle?
	bool mUsingExternalView;
	/// Is this using an external view controller handle?
	bool mUsingExternalViewController;

	// iOS 4 content scaling
	bool mIsContentScalingSupported;
	float mContentScalingFactor;
	float mCurrentOSVersion;

	EAGLSupport* mGLSupport;
	EAGLESContext* mContext;
#ifdef __OBJC__
	NativeWindowType mWindow;
	EAGLView *mView;
	EAGLViewController *mViewController;
#endif

	void switchFullScreen(bool fullscreen) { }
	void getLeftAndTopFromNativeWindow(int & left, int & top, uint width, uint height);
	void initNativeCreatedWindow(const NameValuePairList *miscParams);
	void createNativeWindow(int &left, int &top, uint &width, uint &height, U2String &title);
	void reposition(int left, int top);
	void resize(unsigned int width, unsigned int height);
	void windowMovedOrResized();
	virtual void _beginUpdate();

public:
	EAGLWindow(EAGLSupport* glsupport);
	virtual ~EAGLWindow();

	void create(const U2String& name, unsigned int width, unsigned int height,
				bool fullScreen, const NameValuePairList *miscParams);

	virtual void setFullscreen(bool fullscreen, uint width, uint height);
	void destroy(void);
	bool isClosed(void) const { return mClosed; }
	bool isVisible(void) const { return mVisible; }

	void setVisible(bool visible) { mVisible = visible; }
	void setClosed(bool closed) { mClosed = closed; }
	void swapBuffers(bool waitForVSync);
	void copyContentsToMemory(const U2PixelBox &dst, FrameBuffer buffer);

	/**
	   @remarks
	   * Get custom attribute; the following attributes are valid:
	   * WINDOW         The NativeWindowType target for rendering.
	   * VIEW           The EAGLView object that is drawn into.
	   * VIEWCONTROLLER The UIViewController used for handling view rotation.
	   * GLCONTEXT      The Ogre GLESContext used for rendering.
	   * SHAREGROUP     The EAGLShareGroup object associated with the main context.
	   */
	virtual void getCustomAttribute(const U2String& name, void* pData);

	bool requiresTextureFlipping() const { return false; }
};


U2EG_NAMESPACE_END

#endif
